///===== 新闻管理-news_all.js


//触发列表刷新事件
function reloadList(fromListId) {
    doReloadList({pageIndex: 1, node: {id: fromListId}});
}

//列表刷新
function doReloadList(evt) {
    var list = h3ui.widgets[evt.node.id];

    var queryId = list.childIds.find(a => a.includes("ModelQueryForm"));

    var filter = h3ui.widgets[queryId].props.value || {};

    filter = JSON.parse(JSON.stringify(filter));

    var pageId = list.childIds.find(a => a.includes("Pagination"));

    var pageIndex = h3ui.widgets[pageId].props.current;
    var pageSize = h3ui.widgets[pageId].props.pageSize;


    list.emit("load", {pageIndex, pageSize, filter});


    list.props.selectedRowKeys = [];
    list.props.selectedRow = [];

}


//打开新增弹框
function openAddModal() {
    actionClick({data: {action: "添加"}});
}

//第一次初始化判断
var initnews = true;
var fieldRequired = ["title", "summary", "type", "enabled", "image", "content"]

// 操作栏
function actionClick(evt) {

    console.log(evt);
    var {action, row} = evt.data;

    var modal = h3ui.widgets.CustomModal1;
    var dataSource = h3ui.models.t_news;
    //第一次初始化
    if (initnews) {

        dataSource.schema.properties.filter(a => fieldRequired.includes(a.code)).forEach(a => a.required = true);
        initnews = false;
        modal.props.width = (0.85 * window.innerWidth);
        modal.props.height = (0.85 * window.innerHeight) - 50;
    }

    dataSource.newItem();
    // 赋值
    if (action == "详情" || action == "编辑") {
        //图片转换
        row.image.forEach(b => {
            b.uid = b.id;
            b.status = "done";
            b.url = "/api/file/download?refId=" + b.id;
        })
        dataSource.item = row;
    } else if (action != "添加") {
        return;
    }
    //开启编辑
    h3ui.widgets.FormModel1.props.editMode = !(action == "详情");
    //显示底部按钮
    modal.props.showFooter = !(action == "详情");
    modal.props.title = "新闻".concat(action);
    modal.props.visible = true;
}

//新闻数据新增提交
function submitNews(evt) {
    console.log(evt);

    var title = evt.node.props.title;
    var submitService;
    if (title == "新闻添加") {
        submitService = h3ui.models.t_news.createBO;
    } else {
        submitService = h3ui.models.t_news.updateBO;
    }
    submitService().then(res => {
        h3ui.$message.info("提交成功");
        reloadList("FormList3");
        h3ui.widgets.CustomModal1.props.visible = false;
    })
}

//删除事件
function deletes(evt) {
    console.log(evt);
    var keys = evt.data.keys;
    var schemaCode = evt.node.props.bindingModel.schemaCode;
    var listId = evt.node.id;
    if (keys.length < 1) {
        h3ui.$message.info("请勾选行数据");
        return;
    }
    h3ui.models[schemaCode].batchDeleteBO({bizObjectIds: keys}).then(() => {
        h3ui.$message.info('成功');
        // 刷新页面
        reloadList(listId);
    });
}


//起禁用
async function enabledButton(evt) {

    var rows = h3ui.widgets.FormList3.props.selectedRow;

    var enabled = evt.data.name == "启用" ? true : false;

    var arr = rows.filter(a => new Boolean(a.enabled) != enabled);

    if (arr.length == 0) {
        return;
    }
    var count = 0;

    for (var i = 0; i < arr.length; i++) {
        try {
            await h3ui.models.t_news.updateBO({data: {id: arr[i].id, enabled: enabled}}).then(() => ++count);
        } catch (error) {
            console.log(evt.data.name + "失败,id:" + arr[i].id);
        }
    }

    if (count > 0) {
        reloadList("FormList3");
    }
}